﻿using Engine;
using Game.NetWork;
using Game.NetWork.Packages;
using Game.Server;
using Game.Server.Event;
using Game.Server.PlayerEvent;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Game.Plugin.ChestLock
{
    public class ChestEventHandle : IPlayerBreakAndPlaceHandle, IPlayerInventoryHandle, IPlayerInventoryOpenHandle, IExplodeEventHandle
    {
        public byte FirstLevel => 0;

        public void Explode(int x, int y, int z, ref float pressure, bool isIncendiary, bool noExplosionSound, PlayerData miner)
        {
            var lockData = ChestLockCore.GetLockData(new Point3(x, y, z));
            if (lockData != null)
            {
                pressure = 0;
            }
        }

        public bool PlayerBreakEvent(ComponentPlayer componentPlayer, Point3 point, int digBlockValue, int toolLevel)
        {
            var lockData = ChestLockCore.GetLockData(point);
            var playerData = componentPlayer.PlayerData;
            if (lockData != null)
            {
                if (AuthLevelManager.GetUserLevel(playerData.Client.CommunityAccountId) >= 100)
                {
                    var messagePackget = new MessagePackage("lock", $"这是 {lockData.Username} 的箱子", 0, null);
                    messagePackget.To = playerData.Client; ;
                    CommonLib.Net.QueuePackage(messagePackget);
                    ChestLockCore.RemoveLock(point);
                    return true;
                }
                if (lockData.UserId != playerData.Client.CommunityAccountId)
                {
                    var messagePackget = new MessagePackage("lock", $"走开，这是 {lockData.Username} 的箱子，不是你的", 0, null);
                    messagePackget.To = playerData.Client; ;
                    CommonLib.Net.QueuePackage(messagePackget);
                    return false;
                }
                else
                {
                    ChestLockCore.RemoveLock(point);
                    var messagePackget = new MessagePackage("lock", "上锁的箱子被破坏，失去效果", 0, null);
                    messagePackget.To = playerData.Client; ;
                    CommonLib.Net.QueuePackage(messagePackget);
                }
            }
            return true;
        }

        public bool PlayerDrapDrop(ComponentPlayer componentPlayer, Vector3 worldPos, InventoryDragData inventoryDragData, int count)
        {
            return true;
        }

        public bool PlayerDrop(ComponentPlayer componentPlayer)
        {
            return true;
        }

        public bool PlayerHandleDragDrop(ComponentPlayer componentPlayer, IInventory sourceInventory, int sourceSlotIndex, DragMode dragMode, IInventory targetInventory, int targetSlotIndex, bool processingOnly)
        {
            if (AuthLevelManager.GetUserLevel(componentPlayer.PlayerData.Client.CommunityAccountId) >= 100)
            {
                return true;
            }
            if (sourceInventory is ComponentInventoryBase componentInventoryBase)
            {
                var entity = componentInventoryBase.Entity.FindComponent<ComponentBlockEntity>();
                if (entity == null)
                {
                    return true;
                }
                var playerData = componentPlayer.PlayerData;

                // =========================================================
                var lockData = ChestLockCore.GetLockData(entity.Coordinates);
                if (lockData != null)
                {
                    if (lockData.UserId != playerData.Client.CommunityAccountId)
                    {
                        var messagePackget = new MessagePackage("lock", $"走开，这是 {lockData.Username} 的箱子，不是你的", 0, null);
                        messagePackget.To = playerData.Client; ;
                        CommonLib.Net.QueuePackage(messagePackget);
                        return false;
                    }
                }
            }
            return true;
        }

        public bool PlayerHandleMoveItem(ComponentPlayer componentPlayer, IInventory sourceInventory, int sourceSlotIndex, IInventory targetInventory, int targetSlotIndex, int count)
        {
            if (AuthLevelManager.GetUserLevel(componentPlayer.PlayerData.Client.CommunityAccountId) >= 100)
            {
                return true;
            }
            if (sourceInventory is ComponentInventoryBase componentInventoryBase)
            {
                var entity = componentInventoryBase.Entity.FindComponent<ComponentBlockEntity>();
                if (entity == null)
                {
                    return true;
                }
                var playerData = componentPlayer.PlayerData;

                // =========================================================
                var lockData = ChestLockCore.GetLockData(entity.Coordinates);
                if (lockData != null)
                {
                    if (lockData.UserId != playerData.Client.CommunityAccountId)
                    {
                        var messagePackget = new MessagePackage("lock", $"走开，这是 {lockData.Username} 的箱子，不是你的", 0, null);
                        messagePackget.To = playerData.Client; ;
                        CommonLib.Net.QueuePackage(messagePackget);
                        return false;
                    }
                }
            }
            return true;
        }

        public bool PlayerOpenInventoryEvent(PlayerData playerData, IInventory inventory)
        {
            return true;
        }

        public bool PlayerOpenPointInventoryEvent(PlayerData playerData, Point3 point, IInventory inventory)
        {
            if(inventory is ComponentChest)
            {
                var lockData = ChestLockCore.GetLockData(point);
                if (lockData != null)
                {
                    if (AuthLevelManager.GetUserLevel(playerData.Client.CommunityAccountId) >= 100)
                    {
                        var messagePackget = new MessagePackage("lock", $"这是 {lockData.Username} 的箱子", 0, null);
                        messagePackget.To = playerData.Client; ;
                        CommonLib.Net.QueuePackage(messagePackget);
                        return true;
                    }
                    if (lockData.UserId != playerData.Client.CommunityAccountId)
                    {
                        var messagePackget = new MessagePackage("lock", $"走开，这是 {lockData.Username} 的箱子，不是你的", 0, null);
                        messagePackget.To = playerData.Client; ;
                        CommonLib.Net.QueuePackage(messagePackget);
                        return false;
                    }
                }

                IInventory playerInventory = playerData.ComponentPlayer.ComponentMiner.Inventory;
                int slotValue = playerInventory.GetSlotValue(playerInventory.ActiveSlotIndex);
                if (slotValue == 167)
                {
                    int fileY = point.Z >> 8;
                    int fileX = point.X >> 8;
                    if (fileY == 0 && fileX == 0)
                    // if(fileY == 0)
                    {
                        var messagePackget = new MessagePackage("lock", "这里箱子没法上锁", 0, null);
                        messagePackget.To = playerData.Client; ;
                        CommonLib.Net.QueuePackage(messagePackget);
                        return false;
                    }
                    if (lockData != null)
                    {
                        var messagePackget = new MessagePackage("lock", "箱子已经上锁了", 0, null);
                        messagePackget.To = playerData.Client; ;
                        CommonLib.Net.QueuePackage(messagePackget);
                        return false;
                    }
                    int num3 = playerInventory.RemoveSlotItems(count: 1, slotIndex: playerInventory.ActiveSlotIndex);
                    if(num3 != 0)
                    {
                        var theLockData = new ChestLockCore.LockData();
                        theLockData.Point = point;
                        theLockData.UserId = playerData.Client.CommunityAccountId;
                        theLockData.Username = playerData.Name;
                        ChestLockCore.AddLock(theLockData);
                        var messagePackget = new MessagePackage("lock", "成功为箱子上锁", 0, null);
                        messagePackget.To = playerData.Client; ;
                        CommonLib.Net.QueuePackage(messagePackget);
                        return false;
                    }
                }
            }
            return true;
        }

        public bool PlayerPlaceEvent(ComponentPlayer componentPlayer, Point3 point, int placeBlockValue)
        {
            var lockData = ChestLockCore.GetLockData(point);
            var playerData = componentPlayer.PlayerData;
            if (lockData != null)
            {
                if (AuthLevelManager.GetUserLevel(playerData.Client.CommunityAccountId) >= 100)
                {
                    var messagePackget = new MessagePackage("lock", $"这是 {lockData.Username} 的箱子", 0, null);
                    messagePackget.To = playerData.Client; ;
                    CommonLib.Net.QueuePackage(messagePackget);
                    ChestLockCore.RemoveLock(point);
                    return true;
                }
                if (lockData.UserId != playerData.Client.CommunityAccountId)
                {
                    var messagePackget = new MessagePackage("lock", $"走开，这是 {lockData.Username} 的箱子，不是你的", 0, null);
                    messagePackget.To = playerData.Client; ;
                    CommonLib.Net.QueuePackage(messagePackget);
                    return false;
                }
                else
                {
                    ChestLockCore.RemoveLock(point);
                    var messagePackget = new MessagePackage("lock", "上锁的箱子被破坏，失去效果", 0, null);
                    messagePackget.To = playerData.Client; ;
                    CommonLib.Net.QueuePackage(messagePackget);
                }
            }
            return true;
        }
    }
}